package com.ldk.framework.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alipay.util.AlipayCore;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.Alipay;
import com.ldk.framework.entity.Driver;
import com.ldk.framework.entity.DriverReceiveTrip;
import com.ldk.framework.entity.PlatformFundFlowRecord;
import com.ldk.framework.entity.PushMessage;
import com.ldk.framework.entity.RechargeRecord;
import com.ldk.framework.entity.ServiceTrip;
import com.ldk.framework.entity.ServiceTripCrews;
import com.ldk.framework.entity.Tenpay;
import com.ldk.framework.entity.TripRefundRecord;
import com.ldk.framework.entity.TripStatusRecord;
import com.ldk.framework.entity.User;
import com.ldk.framework.enums.TripNoType;
import com.ldk.framework.enums.TripStatus;
import com.ldk.framework.enums.FlowInOut;
import com.ldk.framework.enums.FlowPurpose;
import com.ldk.framework.enums.PayType;
import com.ldk.framework.enums.PlatformUserType;
import com.ldk.framework.enums.RechargeFrom;
import com.ldk.framework.enums.RechargeType;
import com.ldk.framework.enums.TradePayStatus;
import com.ldk.framework.enums.TripCrewStatus;
import com.ldk.framework.enums.TripRefundStatus;
import com.ldk.framework.push.CustomMsgType;
import com.ldk.framework.push.CustomMsgModule;
import com.ldk.framework.push.PushCustomMsg;
import com.ldk.framework.push.PushTargetApp;
import com.ldk.framework.service.IBCServiceTripService;
import com.weixin.api.WxMpService;
import com.weixin.api.WxMpServiceImpl;
import com.weixin.bean.RefundResData;

/**
 * 
 * @author xiangwl
 * 2017年3月10日
 * com.ldk.framework.service.impl
 * BCServiceTripService
 */
@Service
public class BCServiceTripService extends ServiceTripService implements IBCServiceTripService {
	
	@Override
	@Transactional
	public Boolean publish(final String dbKey, final ServiceTrip entity) throws Exception {
		if (entity == null)
			return false;

		Boolean success = false;
		int row = insert(dbKey, entity);
		if (row > 0) {
			//添加状态记录
			TripStatusRecord record = new TripStatusRecord();
			record.setOrderNo(entity.getTripNo());
			record.setStatusName(TripStatus.Publish.getName());
			record.setStatusValue(TripStatus.Publish.getValue());
			record.setTip(TripStatus.Publish.getTip());
			record.setTime(SystemUtil.getAllSeconds());

			row = tripStatusRecordService.insert(dbKey, record); 

			//添加司机可以接的行程记录
			DriverReceiveTrip receiveTrip = new DriverReceiveTrip();
			receiveTrip.setDriverOpenId(entity.getDriverOpenId());
			receiveTrip.setTripNo(entity.getTripNo());
			driverReceiveTripService.insert(dbKey, receiveTrip);
		}
		success = row > 0;
		return success;
	}

	@Override
	@Transactional
	public Boolean userCancel(final String dbKey, final ServiceTrip trip, User user) throws Exception {
		if (trip == null || StringUtils.isBlank(trip.getTripNo()))
			return false;
		
		final String tripNo = trip.getTripNo();
		final String openId = trip.getOpenId();
		
		ServiceTrip entity = new ServiceTrip();
		entity.setTripNo(tripNo);
		entity.setStatus(TripStatus.UserCancel.getValue());
		entity.setCompleteTime(SystemUtil.getAllSeconds());
		
		int row = updateByKey(dbKey, entity);
		if(row > 0){
			//删除所有可以接此行程的司机记录
			DriverReceiveTrip receiveTrip = new DriverReceiveTrip();
			receiveTrip.setTripNo(tripNo);
			driverReceiveTripService.delete(dbKey, receiveTrip);
			
			TripStatusRecord statusRecord = new TripStatusRecord();
			statusRecord.setOrderNo(entity.getTripNo());
			statusRecord.setStatusName(TripStatus.UserCancel.getName());
			statusRecord.setStatusValue(TripStatus.UserCancel.getValue());
			statusRecord.setTip(TripStatus.UserCancel.getTip());
			statusRecord.setTime(SystemUtil.getAllSeconds());
			
			row = tripStatusRecordService.insert(dbKey, statusRecord);
			//修改用户取消包车业务的次数
			long cancelBcTimes = user.getCancelBcTimes() == null ? 0 : user.getCancelBcTimes();
			User userEntity = new User();
			userEntity.setUserId(user.getUserId());
			userEntity.setCancelBcTimes(cancelBcTimes+1);
			userService.updateByKey(dbKey, userEntity);
			
			//如果司机已经接单了，则要修改司机行程里面的乘客行程状态为取消
			if(!StringUtils.isBlank(trip.getDriverOpenId())){
				ServiceTripCrews crewsQuery = new ServiceTripCrews();
				crewsQuery.setDriverOpenId(trip.getDriverOpenId());
				crewsQuery.setCrewTripNo(tripNo);
				crewsQuery.setCrewOpenId(openId);
				
				ServiceTripCrews tripCrews = serviceTripCrewsService.selectOne(dbKey, crewsQuery);
				if(tripCrews != null){
					crewsQuery = new ServiceTripCrews();
					crewsQuery.setTripCrewId(tripCrews.getTripCrewId());
					crewsQuery.setCanceled(true);
					serviceTripCrewsService.updateByKey(dbKey, crewsQuery);
					
					//司机行程的状态也要同步更新
					ServiceTrip driverTrip = new ServiceTrip();
					driverTrip.setTripNo(tripCrews.getDriverTripNo());
					driverTrip.setStatus(TripStatus.UserCancel.getValue());
					driverTrip.setCompleteTime(SystemUtil.getAllSeconds());
					
					updateByKey(dbKey, driverTrip);
					//同时添加司机行程状态记录
					statusRecord = new TripStatusRecord();
					statusRecord.setOrderNo(tripCrews.getDriverTripNo());
					statusRecord.setStatusName(TripStatus.UserCancel.getName());
					statusRecord.setStatusValue(TripStatus.UserCancel.getValue());
					statusRecord.setTip(TripStatus.UserCancel.getTip());
					statusRecord.setTime(SystemUtil.getAllSeconds());
					tripStatusRecordService.insert(dbKey, statusRecord);
				}
			}
			//定金已经支付成功
			if(trip.getDepositPayFee() != null && trip.getDepositPayStatus() != null && trip.getDepositPayStatus() == TradePayStatus.TRADE_SUCCESS.getValue()){
				//如果司机已接单,需要把定金给司机
				if(trip.getStatus() >= TripStatus.Accept.getValue() && !StringUtils.isBlank(trip.getDriverOpenId())){
					Driver driver = driverService.getDriverByOpenId(dbKey, trip.getDriverOpenId());
					if(driver != null){
						double beforeBalance = driver.getBalance();
						double depositPayFee = trip.getDepositPayFee() == null ? 0 : trip.getDepositPayFee();
						double afterBalance = beforeBalance + depositPayFee;
						Driver driverEntity = new Driver();
						driverEntity.setDriverId(driver.getDriverId());
						driverEntity.setBalance(afterBalance);
						int row1 = driverService.updateByKey(dbKey, driverEntity);
						if(row1 > 0){
							//增加司机充值记录和平台资金流动记录
							//司机余额充值记录
							RechargeRecord record = new RechargeRecord();
							record.setRecordNo(SystemUtil.generateRandomNo(""));
							record.setPhoneNo(driver.getOpenId());
							record.setUserType(PlatformUserType.DRIVER_USER.getValue());
							record.setRechargeMoney(depositPayFee);
							record.setRechargeFrom(RechargeFrom.FROM_SYSTEM.getValue());
							record.setBeforeBalance(beforeBalance);
							record.setAfterBalance(afterBalance);
							record.setRechargeTime(SystemUtil.getAllSeconds());
							record.setRechargeType(RechargeType.CompleteTrip.getValue());
							record.setOperator("system");
							record.setRemark("用户取消包车行程后将用户支付定金金额转入司机余额充值");
							record.setLinkTaskNo(tripNo);
							rechargeRecordService.insert(dbKey, record);
							
							//添加平台流水出账记录
							PlatformFundFlowRecord flowRecord = new PlatformFundFlowRecord();
							flowRecord.setRecordNo(SystemUtil.generateRandomNo(""));
							flowRecord.setUserUniqueId(driver.getOpenId());
							flowRecord.setUserType(PlatformUserType.DRIVER_USER.getValue());
							flowRecord.setFlowAmount(depositPayFee);
							flowRecord.setInOut(FlowInOut.OUT.getValue());
							flowRecord.setFlowFrom("平台"+trip.getDepositPayName());
							flowRecord.setFlowTo("司机余额");
							flowRecord.setFlowMode(trip.getDepositPayId());
							flowRecord.setLinkNo(tripNo);
							flowRecord.setPurpose(FlowPurpose.USER_CANCEL_TRIP_DEPOSIT.getName());
							flowRecord.setRemark("用户支付定金扣给司机");
							platformFundFlowRecordService.insert(dbKey, flowRecord);
						}
						
					}
				}
				else{//未接单,退回用户的费用
					//判断任务支付方式执行退款功能
					Boolean refundSucceed = false;
					int refundStatus = TripRefundStatus.FAILED.getValue();
					//余额支付
					if(trip.getDepositPayId() == PayType.Balance.getId()){
						if(user != null){
							double beforeBalance = user.getBalance();
							double afterBalance = user.getBalance() + trip.getDepositPayFee();
							userEntity = new User();
							userEntity.setUserId(user.getUserId());
							userEntity.setBalance(afterBalance);
							int row1 = userService.updateByKey(dbKey, userEntity);
							refundSucceed = row1 > 0;
							logger.error("userCancel.Balance.refundSucceed="+refundSucceed);
							if(refundSucceed){
								//添加用户充值记录
								RechargeRecord record = new RechargeRecord();
								record.setRecordNo(SystemUtil.generateRandomNo(""));
								record.setPhoneNo(user.getOpenId());
								record.setUserType(PlatformUserType.CLIENT_USER.getValue());
								record.setRechargeMoney(trip.getDepositPayFee());
								record.setRechargeFrom(RechargeFrom.FROM_SYSTEM.getValue());
								record.setBeforeBalance(beforeBalance);
								record.setAfterBalance(afterBalance);
								record.setRechargeTime(SystemUtil.getAllSeconds());
								record.setRechargeType(RechargeType.BalanceRefund.getValue());
								record.setOperator("system");
								record.setRemark("用户取消包车行程退款，余额退款充值");
								record.setLinkTaskNo(tripNo);
								rechargeRecordService.insert(dbKey, record);
							}
							refundStatus = refundSucceed ? TripRefundStatus.SUCCEESS.getValue() : TripRefundStatus.FAILED.getValue();
							
						}
					}//微信支付
					else if(trip.getDepositPayId() == PayType.Tenpay.getId()){
						//获取腾讯支付记录
						String payTradeNo = trip.getDepositPayTradeNo();
						Tenpay query = new Tenpay();
						query.setTransactionId(payTradeNo);
						
						Tenpay tenpay = tenpayService.selectOne(dbKey, query);
						if(tenpay != null){
							WxMpService wxMpService = new WxMpServiceImpl();
							Double totalFee = tenpay.getTotalFee() * 100;
							int fee = totalFee.intValue();
							RefundResData refundResData = wxMpService.refund(tenpay.getOutTradeNo(), tenpay.getTransactionId(),"", "", fee, fee, tenpay.getFeeType());
							
							refundSucceed = refundResData.isResultSuccess();
						}
						refundStatus = refundSucceed ? TripRefundStatus.SUCCEESS.getValue() : TripRefundStatus.FAILED.getValue();

						logger.error("userCancel.Tenpay.refundSucceed="+refundSucceed);
					}//支付宝支付
					else if(trip.getDepositPayId() == PayType.Alipay.getId()){
						//新的支付宝退款流程：直接调接口退款
						String payTradeNo = trip.getDepositPayTradeNo();
						Alipay query = new Alipay();
						query.setTradeNo(payTradeNo);
						
						Alipay alipay = alipayService.selectOne(dbKey, query);
						if(alipay != null){
							refundSucceed = AlipayCore.refund(alipay.getOutTradeNo(), alipay.getTradeNo(), alipay.getTotalFee().floatValue(), "user cancel order refund", "", "", "", "");
							refundStatus = refundSucceed ? TripRefundStatus.SUCCEESS.getValue() : TripRefundStatus.FAILED.getValue();
							if(refundSucceed){
								Alipay refundAlipay = new Alipay();
								refundAlipay.setPayId(alipay.getPayId());
								refundAlipay.setRefundFee(alipay.getTotalFee());
								refundAlipay.setTradeStatus("TRADE_CLOSED");
								
								alipayService.updateByKey(dbKey, refundAlipay);
							}
						}
						logger.error("userCancel.Alipay.refundSucceed="+refundSucceed);
					
					}
					TripRefundRecord refundRecord = new TripRefundRecord();
					refundRecord.setTaskNo(tripNo);
					refundRecord.setRefundFee(trip.getDepositPayFee());
					refundRecord.setTaskPayId(trip.getDepositPayId());
					refundRecord.setTaskPayTradeNo(trip.getDepositPayTradeNo());
					refundRecord.setRefundStatus(refundStatus);
					refundRecord.setRefundTime(SystemUtil.getAllSeconds());
					//设置备注为平台资金流动用途
					refundRecord.setRemark(FlowPurpose.USER_CANCEL_TRIP_REFUND.getName());
					//添加任务退款记录
					tripRefundRecordService.insert(dbKey, refundRecord);
					
					//退款成功后添加平台资金流动记录,支付宝的再后台处理后在添加这个记录
					if(refundSucceed){
						PlatformFundFlowRecord flowRecord = new PlatformFundFlowRecord();
						flowRecord.setRecordNo(SystemUtil.generateRandomNo(""));
						flowRecord.setUserUniqueId(trip.getOpenId());
						flowRecord.setUserType(PlatformUserType.CLIENT_USER.getValue());
						flowRecord.setFlowAmount(trip.getPayFee());
						flowRecord.setInOut(FlowInOut.OUT.getValue());
						flowRecord.setFlowFrom("平台"+trip.getDepositPayName());
						flowRecord.setFlowTo("用户"+trip.getDepositPayName());
						flowRecord.setFlowMode(trip.getDepositPayId());
						flowRecord.setLinkNo(tripNo);
						flowRecord.setPurpose(FlowPurpose.USER_CANCEL_TRIP_REFUND.getName());
						flowRecord.setRemark("用户支付定金退还给用户");
						
						platformFundFlowRecordService.insert(dbKey, flowRecord);
					}
					
				}
			}
			
			//如果支付成功，且状态为支付完成的状态，需要执行退款功能。
			if (trip.getPayFee() != null && trip.getPayStatus() != null && trip.getPayStatus() == TradePayStatus.TRADE_SUCCESS.getValue()) {
				//判断任务支付方式执行退款功能
				Boolean refundSucceed = false;
				int refundStatus = TripRefundStatus.FAILED.getValue();
				//余额支付
				if(trip.getPayId() == PayType.Balance.getId()){
					if(user != null){
						double beforeBalance = user.getBalance();
						double afterBalance = user.getBalance() + trip.getPayFee();
						userEntity = new User();
						userEntity.setUserId(user.getUserId());
						userEntity.setBalance(afterBalance);
						int row1 = userService.updateByKey(dbKey, userEntity);
						refundSucceed = row1 > 0;
						if(refundSucceed){
							//添加用户充值记录
							RechargeRecord record = new RechargeRecord();
							record.setRecordNo(SystemUtil.generateRandomNo(""));
							record.setPhoneNo(user.getOpenId());
							record.setUserType(PlatformUserType.CLIENT_USER.getValue());
							record.setRechargeMoney(trip.getPayFee());
							record.setRechargeFrom(RechargeFrom.FROM_SYSTEM.getValue());
							record.setBeforeBalance(beforeBalance);
							record.setAfterBalance(afterBalance);
							record.setRechargeTime(SystemUtil.getAllSeconds());
							record.setRechargeType(RechargeType.BalanceRefund.getValue());
							record.setOperator("system");
							record.setRemark("用户取消包车行程退款，余额退款充值");
							record.setLinkTaskNo(tripNo);
							rechargeRecordService.insert(dbKey, record);
						}
						refundStatus = refundSucceed ? TripRefundStatus.SUCCEESS.getValue() : TripRefundStatus.FAILED.getValue();
					}
				}//微信支付
				else if(trip.getPayId() == PayType.Tenpay.getId()){
					//获取腾讯支付记录
					String payTradeNo = trip.getPayTradeNo();
					Tenpay query = new Tenpay();
					query.setTransactionId(payTradeNo);
					
					Tenpay tenpay = tenpayService.selectOne(dbKey, query);
					if(tenpay != null){
						WxMpService wxMpService = new WxMpServiceImpl();
						Double totalFee = trip.getPayFee() * 100;
						int fee = totalFee.intValue();
						RefundResData refundResData = wxMpService.refund(tenpay.getOutTradeNo(), tenpay.getTransactionId(),"", "", fee, fee, tenpay.getFeeType());
						
						refundSucceed = refundResData.isResultSuccess();
					}
					refundStatus = refundSucceed ? TripRefundStatus.SUCCEESS.getValue() : TripRefundStatus.FAILED.getValue();
					
				}//支付宝支付
				else if(trip.getPayId() == PayType.Alipay.getId()){
					//新的支付宝退款流程：直接调接口退款
					String payTradeNo = trip.getPayTradeNo();
					Alipay query = new Alipay();
					query.setTradeNo(payTradeNo);
					
					Alipay alipay = alipayService.selectOne(dbKey, query);
					if(alipay != null){
						refundSucceed = AlipayCore.refund(alipay.getOutTradeNo(), alipay.getTradeNo(), alipay.getTotalFee().floatValue(), "user cancel order refund", "", "", "", "");
						refundStatus = refundSucceed ? TripRefundStatus.SUCCEESS.getValue() : TripRefundStatus.FAILED.getValue();
						if(refundSucceed){
							Alipay refundAlipay = new Alipay();
							refundAlipay.setPayId(alipay.getPayId());
							refundAlipay.setRefundFee(alipay.getTotalFee());
							refundAlipay.setTradeStatus("TRADE_CLOSED");
							
							alipayService.updateByKey(dbKey, refundAlipay);
						}
					}
					logger.error("userCancelTask.alipay::refundSucceed"+refundSucceed+",refundStatus="+refundStatus);
				
				}
				TripRefundRecord refundRecord = new TripRefundRecord();
				refundRecord.setTaskNo(tripNo);
				refundRecord.setRefundFee(trip.getPayFee());
				refundRecord.setTaskPayId(trip.getPayId());
				refundRecord.setTaskPayTradeNo(trip.getPayTradeNo());
				refundRecord.setRefundStatus(refundStatus);
				refundRecord.setRefundTime(SystemUtil.getAllSeconds());
				//设置备注为平台资金流动用途
				refundRecord.setRemark(FlowPurpose.USER_CANCEL_TRIP_REFUND.getName());
				//添加任务退款记录
				tripRefundRecordService.insert(dbKey, refundRecord);
				
				//退款成功后添加平台资金流动记录,支付宝的再后台处理后在添加这个记录
				if(refundSucceed){
					PlatformFundFlowRecord flowRecord = new PlatformFundFlowRecord();
					flowRecord.setRecordNo(SystemUtil.generateRandomNo(""));
					flowRecord.setUserUniqueId(trip.getOpenId());
					flowRecord.setUserType(PlatformUserType.CLIENT_USER.getValue());
					flowRecord.setFlowAmount(trip.getPayFee());
					flowRecord.setInOut(FlowInOut.OUT.getValue());
					flowRecord.setFlowFrom("平台"+trip.getPayName());
					flowRecord.setFlowTo("用户"+trip.getPayName());
					flowRecord.setFlowMode(trip.getPayId());
					flowRecord.setLinkNo(tripNo);
					flowRecord.setPurpose(FlowPurpose.USER_CANCEL_TRIP_REFUND.getName());
					flowRecord.setRemark("用户支付行程费用退还给用户");
					
					platformFundFlowRecordService.insert(dbKey, flowRecord);
				}
				
			}
		}
		//取消成功，则推送
		if(row > 0){
			//推送给用户取消行程成功
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					List<String> aliasList = new ArrayList<String>();
					aliasList.add(openId);
					String toUser = openId;
					String message = String.format("用户取消%s行程", CustomMsgModule.BC.getName());
					String content = String.format("你已成功取消[%s]行程", CustomMsgModule.BC.getName());
					PushCustomMsg customMsg = new PushCustomMsg();
					customMsg.setModule(CustomMsgModule.BC.getValue());
					customMsg.setType(CustomMsgType.UserCancelTrip.getValue());
					customMsg.setKey(tripNo);
					
					PushMessage pushMessage = new PushMessage();
					pushMessage.setContent(content);
					pushMessage.setModule(CustomMsgModule.BC.getValue());
					pushMessage.setTitle(message);
					pushMessage.setTripNo(tripNo);
					pushMessage.setToUser(toUser);
					pushMessage.setMessage(message);
					pushMessage.setContent(content);
					pushMessage.setExtras(customMsg.toJson());
					pushMessage.setPushTime(SystemUtil.getAllSeconds());
					pushMessage.setJumpUrl("");
					
					pushMessageService.pushToUser(dbKey, pushMessage, PushTargetApp.Client, customMsg, aliasList, true);
				}
			}).start();
			//司机接单或用户已经上车的状态都要推送给司机消息
			if (trip.getStatus() >= TripStatus.Accept.getValue() &&
				trip.getStatus() < TripStatus.Complete.getValue()) {
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						List<String> aliasList = new ArrayList<String>();
						aliasList.add(trip.getDriverOpenId());
						String toUser = trip.getDriverOpenId();
						String message = String.format("用户取消%s行程", CustomMsgModule.BC.getName());
						String content = String.format("用户已成功取消[%s]行程,行程单号为：%s",CustomMsgModule.BC.getName(), tripNo);
						PushCustomMsg customMsg = new PushCustomMsg();
						customMsg.setModule(CustomMsgModule.BC.getValue());
						customMsg.setType(CustomMsgType.UserCancelTrip.getValue());
						customMsg.setKey(tripNo);
						
						PushMessage pushMessage = new PushMessage();
						pushMessage.setContent(content);
						pushMessage.setModule(CustomMsgModule.BC.getValue());
						pushMessage.setTitle(message);
						pushMessage.setTripNo(tripNo);
						pushMessage.setToUser(toUser);
						pushMessage.setMessage(message);
						pushMessage.setContent(content);
						pushMessage.setExtras(customMsg.toJson());
						pushMessage.setPushTime(SystemUtil.getAllSeconds());
						pushMessage.setJumpUrl("");
						
						pushMessageService.pushToUser(dbKey, pushMessage, PushTargetApp.Driver, customMsg, aliasList, true);
					}
				}).start();
			}
			
		}
		
		return row > 0;
	}

	
	@Override
	@Transactional
	public synchronized Boolean driverAccept(final String dbKey, final ServiceTrip userTrip) throws Exception {
		if(userTrip == null)
			return false;
		
		ServiceTrip userTripEntity = new ServiceTrip();
		//此参数在外部设置，这里取出来修改
		userTripEntity.setDriverOpenId(userTrip.getDriverOpenId());
		userTripEntity.setTripNo(userTrip.getTripNo());
		userTripEntity.setStatus(TripStatus.Accept.getValue());
		
		int row = updateByKey(dbKey, userTripEntity);
		if(row > 0){
			//添加行程状态
			TripStatusRecord statusRecord = new TripStatusRecord();
			statusRecord.setOrderNo(userTrip.getTripNo());
			statusRecord.setStatusName(TripStatus.Accept.getName());
			statusRecord.setStatusValue(TripStatus.Accept.getValue());
			statusRecord.setTip(TripStatus.Accept.getTip());
			statusRecord.setTime(SystemUtil.getAllSeconds());
			
			row = tripStatusRecordService.insert(dbKey, statusRecord);

			//删除此行程的可接单记录
			DriverReceiveTrip receiveTrip = new DriverReceiveTrip();
			receiveTrip.setTripNo(userTrip.getTripNo());
			driverReceiveTripService.delete(dbKey, receiveTrip);
		}
		if(row > 0){
			//系统创建司机行程，与用户的行程一样，为了与专线和顺风车业务统一
			String driverTripNo = SystemUtil.generateOrderNo(TripNoType.TRIP.getPrefix());
			ServiceTrip driverTrip = new ServiceTrip();
			driverTrip.setTripNo(driverTripNo);
			driverTrip.setServiceCode(userTrip.getServiceCode());
			driverTrip.setUserType(PlatformUserType.DRIVER_USER.getValue());
			driverTrip.setOpenId(userTrip.getDriverOpenId());
			driverTrip.setRoadId(userTrip.getRoadId());
			driverTrip.setStartLocation(userTrip.getStartLocation());
			driverTrip.setStartLon(userTrip.getStartLon());
			driverTrip.setStartLat(userTrip.getStartLat());
			driverTrip.setEndLocation(userTrip.getEndLocation());
			driverTrip.setEndLon(userTrip.getEndLon());
			driverTrip.setEndLat(userTrip.getEndLat());
			driverTrip.setCreateTime(userTrip.getCreateTime());
			driverTrip.setCarRank(userTrip.getCarRank());
			driverTrip.setCarSeats(userTrip.getCarSeats());
			driverTrip.setGoTime(userTrip.getGoTime());
			//一对一满员了
			driverTrip.setStatus(TripStatus.Accept.getValue());
			driverTrip.setCrewCount(1);
			driverTrip.setDistance(userTrip.getDistance());
			driverTrip.setDuration(userTrip.getDuration());
			driverTrip.setBillingMode(userTrip.getBillingMode());
			//费用
			driverTrip.setPayFee(userTrip.getPayFee());
			driverTrip.setDepositPayFee(userTrip.getDepositPayFee());
			//其他默认值
			driverTrip.setHasAsign(false);
			driverTrip.setHasComment(false);
			driverTrip.setHasComplaint(false);
			//添加司机行程
			insert(dbKey, driverTrip);
			//添加行程状态
			TripStatusRecord statusRecord = new TripStatusRecord();
			statusRecord.setOrderNo(userTrip.getTripNo());
			statusRecord.setStatusName(TripStatus.Accept.getName());
			statusRecord.setStatusValue(TripStatus.Accept.getValue());
			statusRecord.setTip(TripStatus.Accept.getTip());
			statusRecord.setTime(SystemUtil.getAllSeconds());
			row = tripStatusRecordService.insert(dbKey, statusRecord);
			
			//添加司机行程人数记录
			ServiceTripCrews crewsEntity = new ServiceTripCrews();
			crewsEntity.setDriverTripNo(driverTripNo);//司机与用户为同一个行程
			crewsEntity.setCrewTripNo(userTrip.getTripNo());
			crewsEntity.setDriverOpenId(userTrip.getDriverOpenId());
			crewsEntity.setCrewOpenId(userTrip.getOpenId());
			crewsEntity.setCrewStatus(TripCrewStatus.OffTheCar.getValue());
			Boolean payStatus = userTrip.getPayStatus() != null &&
					(userTrip.getPayStatus() == TradePayStatus.TRADE_SUCCESS.getValue()
					|| userTrip.getPayStatus() == TradePayStatus.TRADE_FINISHED.getValue());
			crewsEntity.setPayStatus(payStatus);
			crewsEntity.setCanceled(false);
			
			row = serviceTripCrewsService.insert(dbKey, crewsEntity);
		}
		//推送给用户告知司机已接单
		if(row > 0){
			new Thread(new Runnable() {
				@Override
				public void run() {
					//用户行程单号
					String tripNo = userTrip.getTripNo();
					List<String> aliasList = new ArrayList<String>();
					aliasList.add(userTrip.getOpenId());
					String toUser = userTrip.getOpenId();
					String message = String.format("司机接受%s行程", CustomMsgModule.BC.getName());
					String content = String.format("司机已成功接受[%s]行程,行程单号为：%s",CustomMsgModule.BC.getName(), tripNo);
					PushCustomMsg customMsg = new PushCustomMsg();
					customMsg.setModule(CustomMsgModule.BC.getValue());
					customMsg.setType(CustomMsgType.DriverAcceptTrip.getValue());
					customMsg.setKey(tripNo);
					
					PushMessage pushMessage = new PushMessage();
					pushMessage.setContent(content);
					pushMessage.setModule(CustomMsgModule.BC.getValue());
					pushMessage.setTitle(message);
					pushMessage.setTripNo(tripNo);
					pushMessage.setToUser(toUser);
					pushMessage.setMessage(message);
					pushMessage.setContent(content);
					pushMessage.setExtras(customMsg.toJson());
					pushMessage.setPushTime(SystemUtil.getAllSeconds());
					pushMessage.setJumpUrl("");
					
					pushMessageService.pushToUser(dbKey, pushMessage, PushTargetApp.Client, customMsg, aliasList, true);
				}
			}).start();
		}
		
		return row > 0;
	}
}
